package ext.tianma.part.twomaterial.twomaterialInterface;

import java.io.Closeable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFCell;

import ext.tianma.bom.util.ClassificationUtil;
import ext.tianma.bom.util.StringUtil;
import wt.csm.navigation.ClassificationNode;
import wt.fc.WTObject;
import wt.part.WTPart;
import wt.util.WTException;

/**
 * 用于获取二级物料的分类代码
 * 
 * @author laichao_sun
 *
 */
public class TwoMaterialClassifictionCode {

	public static String classIfication_path = "/ptc/Windchill/codebase/ext/tianma/part/config/classifiEName.xlsx";
	public static String ERPPartTemplate_path = "/ptc/Windchill/codebase/ext/generic/erp/config/ERPPartTemplate.xls";

	public static String getClassifictionCode(WTObject object) {
		try {
			if (object instanceof WTPart) {
				WTPart part = (WTPart) object;
				if (!part.getName().contains("胶铁一体")) {
					// 获取分类全路径
					String classification = ClassificationUtil.getClassification(part);
					if (classification.contains("二级物料/")) {
						ClassificationNode node = ClassificationUtil.getClassificationNode(part);
						// 分类名称
						String nodeName = node.getName();
						String EName = getString(classIfication_path, nodeName);

						String code = getString(ERPPartTemplate_path, EName);
						if (StringUtil.isNullOrEmpty(code)) {
							return "";
						}
						return code;
					}
				}else {
					String code = getString(ERPPartTemplate_path, "PC");
					return code;
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return "";
	}

	@SuppressWarnings("unused")
	public static String getString(String path,String name) {
		Workbook we = null;
		InputStream in = null;
		String code ="";
		try {
			in = new FileInputStream(path);
			we = WorkbookFactory.create(in);
			if (null == path || path.equals("")) {
				throw new WTException("没有获取到零部件封样节点提醒的配置路径，请检查!");
			}
			if (null == in || null == we) {
				throw new WTException("没有获取到零部件封样节点提醒的配置路径，请检查");
			}
			if(classIfication_path.equals(path)) {
				code = getCode(we.getSheetAt(0), name);
			}else if(ERPPartTemplate_path.equals(path)) {
				code = getCode2(we.getSheetAt(0), name);
			}
			
			return code;
			
		} catch (WTException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			close(in);
		}
		return "";
	}
	// 通过分类名称来获取英文名称
	private static String getCode(Sheet sheet, String classification) {
		try {
			for (int i = 1; i < sheet.getLastRowNum() + 1; i++) {
				Row row = sheet.getRow(i);
				String temp = getCellValue(row.getCell(0)).trim();
				if (null == temp || temp.equals("")) {
					continue;
				} else {
					if (classification.equals(temp)) {
						String temp2 = getCellValue(row.getCell(1)).trim();
						//System.out.println("SLC ==英文名称为==>"+temp2);
						return temp2;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	// 通过英文名称获取分类代码
		private static String getCode2(Sheet sheet, String classification) {
			try {
				for (int i = 1; i < sheet.getLastRowNum() + 1; i++) {
					Row row = sheet.getRow(i);
					String temp = getCellValue(row.getCell(1)).trim();
					if (null == temp || temp.equals("")) {
						continue;
					} else {
						if (classification.equals(temp)) {
							String temp2 = getCellValue(row.getCell(2)).trim();
							//System.out.println("SLC ==分类代码为==>"+temp2);
							return temp2;
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}
		/**
		 * 获取单元格中的信息
		 * 
		 * @param cell 单元格
		 * @return 主要参数信息
		 * @throws Exception
		 */
		public static String getCellValue(Cell cell) throws WTException {
			String value = "";
			try {
				if (null == cell) {
					return "";
				}
				switch (cell.getCellType()) {
				case XSSFCell.CELL_TYPE_NUMERIC:
					value = Double.toString(cell.getNumericCellValue());
					break;
				case XSSFCell.CELL_TYPE_STRING:
					value = cell.getStringCellValue();
					break;
				case XSSFCell.CELL_TYPE_BLANK:
					value = "";
					break;
				case XSSFCell.CELL_TYPE_BOOLEAN:
					value = Boolean.toString(cell.getBooleanCellValue());
					break;
				case XSSFCell.CELL_TYPE_FORMULA:
					try {
						value = String.valueOf(cell.getNumericCellValue());
					} catch (Exception e) {
						value = cell.getStringCellValue();
					}
					break;
				default:
					break;
				}
			} catch (Exception e) {
			}

			return value.trim();
		}

		/**
		 * 关闭资源
		 * 
		 * @param closeable
		 */
		public static void close(Closeable closeable) {
			try {
				if (closeable != null) {
					closeable.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

}
